package com.hihiwjc.app.zzmh.fragments.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.hihiwjc.app.zzmh.R;
import com.hihiwjc.app.zzmh.adapters.home.RecommendListAdapter;
import com.hihiwjc.app.zzmh.apis.ApiCallback;
import com.hihiwjc.app.zzmh.apis.ApiResult;
import com.hihiwjc.app.zzmh.apis.Apis;
import com.hihiwjc.app.zzmh.apis.home.HomeApi;
import com.hihiwjc.app.zzmh.entities.BaseEntity;
import com.hihiwjc.app.zzmh.entities.home.Recommend;
import com.hihiwjc.app.zzmh.utils.LogUtil;

/**
 * 首页推荐
 * <br/>Created by hihiwjc on 2015/10/26 0026
 * <br/>Author:hihiwjc
 * <br/>Email:hihiwjc@live.com
 */

public class RecommendFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener, ApiCallback {
    private ListView mLvFragmentMainRecommend;
    private RecommendListAdapter mAdapter;
    private SwipeRefreshLayout rootView;

    public RecommendFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = (SwipeRefreshLayout) inflater.inflate(R.layout.fragment_main_recommend, container, false);
        rootView.setProgressViewOffset(true, 0, 40);
        mLvFragmentMainRecommend = (ListView) rootView.findViewById(R.id.lv_fragment_main_recommend_list);
        /*开始刷新首页推荐*/
        rootView.setRefreshing(true);
        rootView.setOnRefreshListener(this);
        onRefresh();
        return rootView;
    }

    /**
     * 刷新首页推荐
     */
    @Override
    public void onRefresh() {
        Apis.get().home(this).reqHomeRecommend();
    }

    private void setupRecommend(Recommend recommend) {
        if (mAdapter != null) {
            mAdapter = new RecommendListAdapter(getContext(), recommend);
        } else {
            mAdapter = null;
            mAdapter = new RecommendListAdapter(getContext(), recommend);
        }
        mLvFragmentMainRecommend.setAdapter(mAdapter);
    }

    @Override
    public void onApiCallback(ApiResult apiResult) {
        if (apiResult.api_id == HomeApi.API_ID) {
            if (apiResult.data != null && apiResult.data instanceof Recommend) {
                switch (apiResult.action_id) {
                    case HomeApi.ACTION_HOME_RECOMMEND:
                        setupRecommend((Recommend) apiResult.data);
                        break;
                }
            } else {
                //数据为空处理
                BaseEntity data = (BaseEntity) apiResult.data;
                if (data != null) {
                    Toast.makeText(getContext(), data.getErrors(), Toast.LENGTH_SHORT).show();
                    LogUtil.e(data.getErrors());
                }
            }
        }
        rootView.setRefreshing(false);
    }
}
